package com.yksj.healthtalk.ui.doctorstation;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.universalimageloader.core.ImageLoader;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.LodingFragmentDialog;
import com.yksj.healthtalk.comm.ZoomImgeDialogFragment;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 退号
 * @author Administrator
 *
 */
public class DoctorSettingExitActivity extends BaseFragmentActivity implements OnClickListener {

	private String clientId;
	private final String LOOK_CONTENT="findcustomerBackOrder";//查看订单
	private final String SUMINT_EXIT="doctorBackOrder";//同意退回
	private final String SUMINT_REFUSE="updateOrderStatus";//拒绝退回
	private String ORIDERID;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_setting_exit);
		initTitle();
		titleTextV.setText("退号");
		titleTextV.setTextColor(Color.BLACK);
		titleLeftBtn.setOnClickListener(this);
		clientId = getIntent().getStringExtra("id");
		ORIDERID=getIntent().getStringExtra("ORIDERID");
		initData(LOOK_CONTENT);
	}

	private void initData(final String type) {
		HttpRestClient.doHttpLookDoctorClientExit(ORIDERID,SmartFoxClient.getLoginUserId(),clientId,type,new AsyncHttpResponseHandler(){
			private LodingFragmentDialog showLodingDialog;
			@Override
			public void onStart() {
				showLodingDialog = LodingFragmentDialog.showLodingDialog(getSupportFragmentManager(), "马上来了...");
			}
			@Override
			public void onSuccess(int statusCode, String content) {
				try {
					if(type.equals(LOOK_CONTENT)){//查看
						initView(content);
					}else if(type.equals(SUMINT_EXIT)){//同意
						JSONObject object=new JSONObject(content);
						if(object.has("error_message")){
							ToastUtil.showBasicShortToast(DoctorSettingExitActivity.this, object.getString("error_message"));
						}else{
								exitRegister(object.optString("message"));
						}
					}else{//拒绝
						JSONObject object=new JSONObject(content);
						if(object.has("error_message")){
							ToastUtil.showBasicShortToast(DoctorSettingExitActivity.this, object.getString("error_message"));
						}else{
							ToastUtil.showToastPanl("已拒绝");
							Intent putExtra = getIntent();
							putExtra.putExtra("isTrue", true);
							setResult(RESULT_OK, putExtra);
							DoctorSettingExitActivity.this.finish();
							overridePendingTransition(R.anim.back_enter,R.anim.back_exit);
						}
					}
				} catch (Exception e) {
					onFailure(null);
				}
			}
			@Override
			public void onFinish() {
				showLodingDialog.dismissAllowingStateLoss();
			}
			@Override
			public void onFailure(Throwable error) {
				ToastUtil.showBasicShortToast(DoctorSettingExitActivity.this, "请稍后重试...");
			}
		});
		
	}

	/**
	 * 同意退号,处理成功后更新多美币
	 */
	protected void exitRegister(String str) {
		ToastUtil.showToastPanl("已退号");
		Intent putExtra = getIntent();
		putExtra.putExtra("isTrue", true);
		setResult(RESULT_OK, putExtra);
		DoctorSettingExitActivity.this.finish();
		overridePendingTransition(R.anim.back_enter,R.anim.back_exit);
	}

	private void initView(String con) {
		try {
			ImageLoader loader=ImageLoader.getInstance();
			JSONObject object=new JSONObject(con);
			findViewById(R.id.refuse).setOnClickListener(this);
			findViewById(R.id.agree).setOnClickListener(this);
			
			TextView content=(TextView)findViewById(R.id.content);
			content.setText(object.getString("BACK_REASON"));
			int pics [] = {R.id.pic1,R.id.pic2,R.id.pic3};
			JSONArray jsonArray = object.getJSONArray("PICS");
			for (int i = 0; i < jsonArray.length(); i++) {
				ImageView findView = (ImageView) findViewById(pics[i]);
				findView.setVisibility(View.VISIBLE);
				JSONObject object2 = (JSONObject)jsonArray.get(i);
				final String path=object2.getString("BACK_PIC");
				loader.displayImage(path, findView);
				findView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						ZoomImgeDialogFragment.show(path,getSupportFragmentManager());
					}
				});
			}
			TextView contentmesga=(TextView)findViewById(R.id.mesgae);
			contentmesga.setText(object.getString("BACK_MESG"));
		} catch (JSONException e) {
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.agree://同意
			initData(SUMINT_EXIT);
			break;
		case R.id.refuse://拒绝
			initData(SUMINT_REFUSE);
			break;
		}
		
	}
}
